package {
	import flash.display.*;
	import flash.events.*;
	import flash.xml.*;
	import flash.net.*;
	
	import mx.collections.*;
	import mx.controls.CheckBox;
	import mx.controls.Tree;
	import mx.controls.listClasses.*;
	import mx.controls.treeClasses.*;
	

	public class CheckboxTree extends TreeItemRenderer{
		protected var myCheckBox:CheckBox;
		static private var STATE_CHECKED:String = "checked";
		static private var STATE_UNCHECKED:String = "unchecked";
        public function CheckboxTree () {
			super();
			mouseEnabled = false;
		}
		private function toggleChildren(item:Object, tree:Tree, state:String):void{
			if (item == null){
				return;
			}else{
				item.@state = state;
				var treeData:ITreeDataDescriptor = tree.dataDescriptor;
				if (treeData.hasChildren(item)){
					var children:ICollectionView = treeData.getChildren (item);
					var cursor:IViewCursor = children.createCursor();
					while (!cursor.afterLast){
						toggleChildren(cursor.current, tree, state);
						cursor.moveNext();
					}
				}
			}
		}
		private function checkBoxToggleHandler(e:MouseEvent):void{
			if (data){
				var myListData:TreeListData = TreeListData(this.listData);
				var selectedNode:Object = myListData.item;
				var tree:Tree = Tree(myListData.owner);
				var toggle:Boolean = myCheckBox.selected;
				if (toggle){
					if(e.type == "doubleClick"){
						toggleChildren(data, tree, STATE_CHECKED);
					}else{
						data.@state = STATE_CHECKED;
					}
				}else{
					if(e.type == "doubleClick"){
						toggleChildren(data, tree, STATE_UNCHECKED);
					}else{
						data.@state = STATE_UNCHECKED;
					}
				}
				tree.dispatchEvent(new Event("selectchange"));
			}
		}
		override protected function createChildren():void{
			super.createChildren();
			myCheckBox = new CheckBox();
			myCheckBox.setStyle( "verticalAlign", "middle" );
			myCheckBox.doubleClickEnabled = true;
			myCheckBox.addEventListener(MouseEvent.CLICK, checkBoxToggleHandler);
			myCheckBox.addEventListener(MouseEvent.DOUBLE_CLICK, checkBoxToggleHandler);
			addChild(myCheckBox);
	    }	
		private function setCheckState(d:Object):void{
			if (d.@state == STATE_CHECKED){
				myCheckBox.selected = true;
			}else{
				myCheckBox.selected = false;
			}
			if(d.@r == "1"){
				myCheckBox.enabled = false;
			}
		}
		override public function set data(value:Object):void{
			super.data = value;
			setCheckState (value);
			if(TreeListData(super.listData).item.@type == 'dimension'){
			    setStyle("fontStyle", 'italic');
			}else{
				if (this.parent != null){
					var _tree:Tree = Tree(this.parent.parent);
		    		_tree.setStyle("defaultLeafIcon", null);
		  		}
				setStyle("fontStyle", 'normal');
			}			
	    }
	   	override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
	        if(super.data){
			    if (super.icon != null){
				    myCheckBox.x = super.icon.x + 3;
				    myCheckBox.y = 9;
				    super.icon.x = myCheckBox.x + myCheckBox.width + 16;
				    super.label.x = super.icon.x + super.icon.width + 3;
				}else{
				    myCheckBox.x = super.label.x + 3;
				    myCheckBox.y = 9;
				    super.label.x = myCheckBox.x + myCheckBox.width + 16;
				}
			}
	    }
	    public function output(str:Object):void{;
			  var lc:LocalConnection = new LocalConnection();
			  lc.addEventListener(StatusEvent.STATUS, function(e:Event):void{});
			  var s:String = str.toString();
			  lc.send("cenfun_lc", "cenfunTrace", s);
		}
	}
}